package com.systop.scos.mail.webapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.common.modules.security.user.service.UserManager;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;
import com.systop.scos.mail.MailConstants;
import com.systop.scos.mail.model.Mail;
import com.systop.scos.mail.model.MailBox;
import com.systop.scos.mail.service.MailBoxManager;
import com.systop.scos.mail.service.MailManager;

@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MailAction extends DefaultCrudAction<Mail, MailManager> {
	
	//其实是MailBox's id.
	private Integer mailId;
	
	//区别显示收件箱还是发件箱, 默认为收件箱
	private String mailIndexType = MailConstants.TYPE_SEND;
	
	//邮件状态（正式邮件、草稿），默认为草稿邮件
	private short mailStatus = MailConstants.N;
	
	//收件人Id
	private String receiverIds;
	
	/** 附件序列,ID以","分割 */
	private String fileattchids;
	
	//查询开始日期
	private Date startDate;
	
	//查询结束日期
	private Date endDate;
	
	@Autowired
	private MailBoxManager mailBoxMgr;
	@Autowired
	private UserManager userManger;
	/**
	 * 新建邮件
	 * @return
	 */
	public String newMail() {
		getModel().setSender(UserUtil.getPrincipal(getRequest()));
		MailBox mailBox = new MailBox();
		mailBox.setUser(userManger.getUser(receiverIds));
		return INPUT;
	}

	/**
	 * 发送邮件
	 * 
	 * @return
	 */
	public String sendMail() {
		Assert.notNull(getUser());
		if (StringUtils.isNotBlank(getModel().getRecipientIDs())){
			Mail mail = getModel();
			if (StringUtils.isNotBlank(fileattchids)){//存附件
				mail.setFileIds(fileattchids);
			}
			//mail.setFileIds(getRequest().getParameter("fileattchids"));//附件赋值
			mail.setSendTime(new Date());
			mail.setMailStatus(MailConstants.Y);//设置邮件状态（正式邮件）
			mail.setSender(getUser());//发件人
			//保存邮件Mail
			getManager().save(mail);
			//在发件箱中保持Mail的引用
			MailBox mailBox = mail2Box(mail, getUser(), MailConstants.TYPE_SEND);
			mailBoxMgr.save(mailBox);
			if (mail.getRecipientIDs() != null){
				String[] recipientIDs = mail.getRecipientIDs().split(",");// 收件人IDs
				sendToUsers(mail, recipientIDs, MailConstants.SEND_NORMAL);//发送
			}
			if(mail.getCopyToIDs() != null){
				String[] copyToIDs = mail.getCopyToIDs().split(",");// 抄送人IDs
				sendToUsers(mail, copyToIDs, MailConstants.SEND_COPY);//抄送
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 存草稿
	 */
	public String saveDraft() {
		Assert.notNull(getUser());
		Mail mail = getModel();
		if (StringUtils.isNotBlank(fileattchids)){//存附件
			mail.setFileIds(fileattchids);
		}
		mail.setSendTime(new Date());//创建日期
		mail.setSender(getUser());
		mail.setMailStatus(MailConstants.N);//设置邮件状态（草稿）
		getManager().save(mail);//保存邮件Mail
		
		return "draftSuccess";
	}
	
	/**
	 * 将邮件发送给用户
	 * @param mail 邮件
	 * @param ids 用户id列表
	 * @param type 类别[发送/抄送]
	 */
	private void sendToUsers(Mail mail, String[] ids, String type) {
		// 发送邮件
		for (String id : ids) {
			if (StringUtils.isNotBlank(id) && StringUtils.isNumeric(id)) {
				User user = getManager().getDao().get(User.class, Integer.valueOf(id));
				MailBox mailBoxSend = mail2Box(mail, user, MailConstants.TYPE_RECEVICE);
				if (MailConstants.SEND_NORMAL.equals(type)){//正常发送的邮件
					mailBoxSend.setNote("send mail");
				}else{//抄送的邮件
					mailBoxSend.setNote("copy send mail");
				}
				mailBoxMgr.save(mailBoxSend);
			}
		}
	}
	
	//根据邮件类型将Mail转换成MailBox
	private MailBox mail2Box(Mail mail, User u, String sendType) {
		MailBox mailBox = null;
		if (mail != null){
			mailBox = new MailBox();
			mailBox.setUser(u);
			mailBox.setMail(mail);
			mailBox.setSendTime(mail.getSendTime());
			mailBox.setType(sendType);
			mailBox.setIsDel(MailConstants.N);
			if (MailConstants.TYPE_RECEVICE.equals(sendType)){//如果是收件箱则设置下列属性
				mailBox.setIsRead(MailConstants.N);
				mailBox.setIsReply(MailConstants.N);
			}
		}
		return mailBox;
	}
	
	/**
	 * 邮件列表
	 */
	public String index(){
		Assert.notNull(getUser());
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer hql = new StringBuffer("from MailBox m where 1 = 1");
		List<Object> args = new ArrayList<Object>();
		hql.append(" and m.user.id = ? and m.type = ? and m.isDel = ?");
		args.add(getUser().getId());
		args.add(this.mailIndexType);
		args.add(MailConstants.N);//未被移除的
		if (getModel().getSubject() != null) {// 根据标题查询
			hql.append(" and m.mail.subject like ?");
			args.add("%" + getModel().getSubject() + "%");
		}
		if (startDate != null && endDate != null) {//根据时间查询
			hql.append(" and (m.sendTime between ? and ?)");
			args.add(startDate);
			args.add(endDate);
		}
		hql.append("order by m.sendTime desc");
		page = mailBoxMgr.pageQuery(page, hql.toString(), args.toArray());
		restorePageData(page);	
		
		return INDEX;
	}
	
	/**
	 * 草稿箱
	 */
	public String indexDraft() {
		Assert.notNull(getUser());
		String hql= "from Mail m where m.sender.id = ? and m.mailStatus = ? order by m.sendTime desc";
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		page = getManager().pageQuery(page, hql, new Object[]{getUser().getId(), this.mailStatus});
		restorePageData(page);
		return "indexDraft";
	}
	/**
	 * 查看邮件[发件箱查看/收件箱查看]
	 */
	public String view() {
		Assert.notNull(getUser());
		MailBox mailbox = mailBoxMgr.get(mailId);
		Assert.notNull(mailbox);
		if (getUser().equals(mailbox.getUser()) && mailbox.getType().equals(MailConstants.TYPE_RECEVICE)){
			mailbox.setIsRead(MailConstants.Y);
		}
		mailBoxMgr.save(mailbox);
		getRequest().setAttribute("mailbox", mailbox);
		return VIEW;
	}
	
	/**
	 * 正式邮件删除.修改isDel值
	 */
	public String removeMailBox() {
		if (selectedItems != null){
			MailBox mailBox = null;
			for(Serializable id : selectedItems){
				mailBox = mailBoxMgr.get(convertId(id));
				mailBox.setIsDel(MailConstants.Y);//设置isDel的值
				mailBoxMgr.save(mailBox);
			}
		}
		return SUCCESS;
	}

	/**草稿删除
	 * 
	 * @return
	 */
	public String remove() {
		getManager().remove(getModel());
		return "draftSuccess";
	}
	
	
	private User getUser() {
		return UserUtil.getPrincipal(getRequest());
	}

	public String getMailIndexType() {
		return mailIndexType;
	}

	public void setMailIndexType(String mailIndexType) {
		this.mailIndexType = mailIndexType;
	}

	public void setMailId(Integer mailId) {
		this.mailId = mailId;
	}

	public String getFileattchids() {
		return fileattchids;
	}

	public void setFileattchids(String fileattchids) {
		this.fileattchids = fileattchids;
	}

	public String getReceiverIds() {
		return receiverIds;
	}

	public void setReceiverIds(String receiverIds) {
		this.receiverIds = receiverIds;
	}

	public short getMailStatus() {
		return mailStatus;
	}

	public void setMailStatus(short mailStatus) {
		this.mailStatus = mailStatus;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	
}
